package com.bsoft.gol.hbs.dao;

import com.bsoft.gol.hbs.utils.CommonUtils;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import hcn.base.NavigationListSettings;
import hcn.service.base.qo.NavigationListSettingQo;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
@SsdevDao(serviceId = "navigationListSettingsDaoService")
public abstract class NavigationListSettingsDAO extends HibernateSupportDelegateDAO<NavigationListSettings> {
    public NavigationListSettingsDAO() {
        this.setKeyField("settingId");
    }


    public List<NavigationListSettings> findNavigationListSettings(final NavigationListSettingQo qo) {
        HibernateStatelessResultAction<List<NavigationListSettings>> action = new AbstractHibernateStatelessResultAction<List<NavigationListSettings>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                String hsql = "from NavigationListSettings a where a.organizationId =:organizationId ";
                Query query = ss.createQuery(hsql);
                query.setParameter("organizationId", qo.getOrgId());
                int first = qo.getPageNo() > 0 ? (qo.getPageNo() - 1) * qo.getPageNo() : 0;
                query.setFirstResult(first);
                query.setMaxResults(qo.getPageSize());
                List<NavigationListSettings> ls =  query.list();
//                List<NavigationListSettings> navigationListSettings = new ArrayList<>();
//                if(CommonUtils.isNotEmpty(ls)){
//                    for (Map map:ls) {
//                        navigationListSettings.add(ConversionUtils.convert(map,NavigationListSettings.class));
//                    }
//                }
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    @DAOMethod(sql= "select count(1) from NavigationListSettings a where a.organizationId =:organizationId")
    public abstract long getNavigationListSettingsCount(@DAOParam("organizationId") String  organizationId);

    @DAOMethod(sql = " from NavigationListSettings where settingId=:settingId ")
    public abstract NavigationListSettings getNavigationListSetting(@DAOParam("settingId")Long settingId);

    @RpcService
    @DAOMethod(sql = " from NavigationListSettings where organizationId=:organizationId and moduleStatus=1")
    public abstract List<NavigationListSettings> findNavigationListSettingByOrgId(@DAOParam("organizationId")String organizationId);

    public List<NavigationListSettings> findNavigationListSettingList(final NavigationListSettingQo qo) {
        HibernateStatelessResultAction<List<NavigationListSettings>> action = new AbstractHibernateStatelessResultAction<List<NavigationListSettings>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                 hsql.append( "from NavigationListSettings a where a.organizationId =:organizationId and a.moduleStatus=1");
                if(CommonUtils.isNotEmpty(qo.getModuleCode())){
                    hsql.append(" and a.moduleCode=:moduleCode ");
                }
                if(CommonUtils.isNotEmpty(qo.getGuideCode())){
                    hsql.append(" and a.guideCode=:guideCode ");
                }
                Query query = ss.createQuery(hsql.toString());
                query.setParameter("organizationId", qo.getOrgId());
                if(CommonUtils.isNotEmpty(qo.getModuleCode())){
                    query.setParameter("moduleCode",qo.getModuleCode());
                }
                if(CommonUtils.isNotEmpty(qo.getGuideCode())){
                    query.setParameter("guideCode",qo.getGuideCode());
                }
                List<NavigationListSettings> navigationListSettings =  query.list();
//                List<NavigationListSettings> navigationListSettings = new ArrayList<>();
//                if(CommonUtils.isNotEmpty(ls)){
//                    for (Map map:ls) {
//                        navigationListSettings.add(ConversionUtils.convert(map,NavigationListSettings.class));
//                    }
//                }
                setResult(navigationListSettings);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

}
